package com.seek.eduservice.controller;


import com.seek.commonutils.ResponseResult;
import com.seek.eduservice.client.VodClient;
import com.seek.eduservice.entity.EduVideo;
import com.seek.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-01-16
 */
@RestController
@RequestMapping("/eduservice/edu-video")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    // 添加小节
    @PostMapping("add")
    public ResponseResult add(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return ResponseResult.ok();
    }

    // 根据小节 id 查询小节
    @GetMapping("getById/{videoId}")
    public ResponseResult getById(@PathVariable String videoId) {
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return ResponseResult.ok().data("video", eduVideo);
    }

    // 根据小节 id 修改小节
    @PutMapping("updateById")
    public ResponseResult updateById(@RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return ResponseResult.ok();
    }

    // 根据小节 id 删除小节，同时删除对应的阿里云中的视频
    @DeleteMapping("deleteById/{videoId}")
    public ResponseResult deleteById(@PathVariable String videoId) {
        EduVideo eduVideo = eduVideoService.getById(videoId);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!(videoSourceId == null || videoSourceId.equals(""))) {
            vodClient.removeAlyVideoByVideoSourceId(videoSourceId);
        }
        eduVideoService.removeById(videoId);
        return ResponseResult.ok();
    }

}

